<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Components\Tools;
use App\Components\DyToools;
use App\Models\DyModels\Douyin;

/**
 * 获取抖音视频
 */
class DyKeywordVideo extends Command {

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'DyKeywordVideo';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '获取抖音视频';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle() {
        Tools::commandsLog('DyKeywordVideo_begin:' . date("Y-m-d H:i:s"));
        $this->searchVideo();
        Tools::commandsLog('DyKeywordVideo_end:' . date("Y-m-d H:i:s"));
    }

    //定时任务 抓取关键词视频  清洗入库--------------------------------------------------------------------------------------------------------------------------------
    public function searchVideo() {
        set_time_limit(0);
        $industry = new Douyin();
        $keywordArr = $industry->selectIndAll(); //关键词列表
        //Tools::commandsLog(json_encode($keywordArr));
        if ($keywordArr) {
            foreach ($keywordArr as $k => $v) {
                $this->whileVideo(0, $v['name']);
                usleep(200000); //休眠
            }
        }
    }

    //循环抓取关键词视频
    public function whileVideo($cursor = 0, $keyword) {
        set_time_limit(0);
        $pt_dy_access_token = new Douyin();
        $DyToools = new DyToools();
        $open_token = $pt_dy_access_token->selectAuthAll(); //已经授权用户
        $open_id = isset($open_token['open_id']) ? $open_token['open_id'] : '';
        $access_token = isset($open_token['access_token']) ? $open_token['access_token'] : '';
        $refresh_token = isset($open_token['refresh_token']) ? $open_token['refresh_token'] : '';

        $url = Tools::getOneconfig("/config/third.json", array('douyin', 'dy_url'));
        $url = "$url/video/search?open_id={$open_id}&access_token={$access_token}&cursor={$cursor}&count=20&keyword={$keyword}";
        $data = $this->getJson($url);

        if ($data['data']['error_code'] == 0) {

            if (isset($data['data']['list'])) {
                $pt_dy_access_token->saveKeyVideo($data['data']['list'], $keyword); //存入数据库
            }
            //翻页继续调用
            if ($data['data']['cursor'] > 0) {
                $this->whileVideo($data['data']['cursor'], $keyword);
            }
        } elseif ($data['data']['error_code'] == 10008 || $data['data']['error_code'] == 2190008) {//token过期
            $dataOne = $DyToools->refresh_token($refresh_token);
            if ($dataOne['data']['error_code'] == 0) {


                $pt_dy_access_token->upToken($dataOne['data'], $open_id); //更新Token
                $this->whileVideo($cursor, $keyword);
            } elseif ($dataOne['data']['error_code'] == 10010) {//refresh 过期
                $dataTwo = $DyToools->renew_refresh_token($refresh_token);
                if ($dataTwo['data']['error_code'] == 0) {

                    $pt_dy_access_token->upRef($dataTwo['data'], $open_id); //更新 refresh
                    $this->whileVideo($cursor, $keyword);
                } else {

                    $pt_dy_access_token->upTokenStatus($open_id); //授权过期需用户重新授权 更改授权状态 为 0
                }
            }
        }
    }

    //get请求
    function getJson($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60); //连接后等待时间
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); //连接前等待时间
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //为true，则会跟踪爬取重定向页面，否则，不会跟踪重定向页面
        curl_setopt($ch, CURLOPT_ENCODING, ""); //返回乱码处理
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书下同
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output, true);
    }

}
